package com.sean.iphoneAppWorkspace;

import java.util.Random;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.MotionEvent;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.TranslateAnimation;
import android.widget.ImageView;

import com.sean.util.UnitUtil;

/**
 * 可拖放的控件
 * @author Sean
 *
 */
public class DragItemView extends ImageView implements Comparable<DragItemView> {
	
	public static final String TAG = "DragItemView";
	
	private static Random RANDOM = new Random();
	private static int[] SHAKE_DEGREES = new int[] {-5, -4, -3, -2, -1, 0,  1, 2, 3, 4, 5};
	
	private boolean mIsShake;
	private boolean mShakeDirection;
	private int mShakeIndex;
	
	private String mText;
	private Drawable mIcon;

	private DataVo mData;
	
	private Paint mPaint;
	private int[] mTextPoint;
	
	private Animation mAnimation;
	
	public DragItemView(Context context, DataVo data, int width) {
		super(context);
		mData = data;
		mText = mData.getText();
		setIcon(mData.getIcon());
		
	}
	
	public DataVo getData() {
		return mData;
	}
	
	public void setData(DataVo data) {
		mData = data;
	}
	
	public void setIcon(int icon) {
		mIcon = this.getResources().getDrawable(icon);
		this.setImageDrawable(mIcon);
	}
	
	public void setText(String text) {
		this.mText = text;
	}

	/**
	 * 设置图标是否摇晃
	 * @param isShake
	 */
	public void setShake(boolean isShake) {
		mIsShake = isShake;
		mShakeIndex = RANDOM.nextInt(11);
	}
	
	public void moveTo(int x, int y, int time, AnimationListener animationListener) {
		int srcLeft = this.getLeft();
		int srcTop = this.getTop();
		int targetLeft = x;
		int targetTop = y;
		mAnimation = new TranslateAnimation(0, targetLeft - srcLeft, 0, targetTop - srcTop);
		mAnimation.setDuration(time);
		mAnimation.setFillAfter(false);
		if(animationListener != null) {
			mAnimation.setAnimationListener(animationListener);
		} else {
			mAnimation.setAnimationListener(new AnimationListener() {
				
				@Override
				public void onAnimationStart(Animation animation) {
					// TODO Auto-generated method stub
					
				}
				
				@Override
				public void onAnimationRepeat(Animation animation) {
					// TODO Auto-generated method stub
					
				}
				
				@Override
				public void onAnimationEnd(Animation animation) {
					DragItemView.this.invalidate();
					
				}
			});
		}
		this.startAnimation(mAnimation);
	}


	@Override
	protected void onDraw(Canvas canvas) {
		int space = (int)UnitUtil.dip2px(this.getContext(), 10);
		
		int iconWidth = mIcon.getIntrinsicWidth();
		int iconHeight = mIcon.getIntrinsicHeight();
		Rect rect = mIcon.getBounds();
		rect.left = this.getMeasuredWidth() / 2 - iconWidth / 2;
		rect.right = rect.left + iconWidth;
		rect.top = space;
		rect.bottom = rect.top + iconHeight;
		
		mPaint = new Paint();
		mPaint.setTextSize(20);
		mPaint.setAntiAlias(true);
		mPaint.setColor(Color.WHITE);
		mTextPoint = new int[2];
		int length = (int)mPaint.measureText(mText);
		mTextPoint[0] = this.getMeasuredWidth() / 2 - length / 2;
		mTextPoint[1] = space * 2 + iconHeight;
		
		//画图标
		canvas.save();
		if(mIsShake) {
			if(mShakeIndex >= SHAKE_DEGREES.length - 1) {
				mShakeDirection = true;
			} else if(mShakeIndex <= 0) {
				mShakeDirection = false;
			}
			canvas.rotate(SHAKE_DEGREES[mShakeIndex], this.getMeasuredWidth() / 2, this.getMeasuredHeight() / 2);
			if(mShakeDirection) {
				mShakeIndex --;
			} else {
				mShakeIndex ++;
			}
		}
		mIcon.draw(canvas);
		canvas.restore();
		
		//画标题
		canvas.drawText(mText, mTextPoint[0], mTextPoint[1], mPaint);
	}
	
//	@Override
//	public boolean onTouchEvent(MotionEvent ev) {
//		
//		Log.i(TAG, "onTouchEvent");
//        
//		//释放则结束拖动的效果
//		switch (ev.getAction()) {
//        case MotionEvent.ACTION_CANCEL:
//        case MotionEvent.ACTION_UP:
//        	Log.i(TAG, "onTouchEvent >> UP");
//        	return false;
//      	}
//		return super.onTouchEvent(ev);
//	}

	@Override
	public int compareTo(DragItemView another) {
		if(another.getData().getPosition() > this.getData().getPosition()) {
			return -1;
		}
		return 1;
	}
	
}
